﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

namespace ZXthex.CodingUI
{
    /// <summary>
    /// 有下拉框提示的输入框
    /// </summary>
    public class InputText: BaseCodingUI
    {
        InputText()
        {
        }

        public InputField inputField { get; private set; }
        public List<string> autoCompletes { get; private set; }


        public void ResizeUI()
        {

            inputField.textComponent.fontSize = UIHelper.GetRecommandFontSizeWithHeight((int)(inputField.transform as RectTransform).rect.height);

        }

        public static InputText Instantiate(Transform parent = null)
        {
            var img = new GameObject("InputField", typeof(RectTransform)).AddComponent<Image>();
            img.sprite = CodingPool.GetSquare();
            img.type = Image.Type.Sliced;

            InputField inf = img.gameObject.AddComponent<InputField>();
            Text text = CreateTextGameObject();
            inf.textComponent = text;
            text.transform.SetParent(inf.transform);

            inf.transform.SetParent(parent);
            SetDefaultRectTransform(inf.transform as RectTransform, -4);



            
            text.alignment = TextAnchor.MiddleLeft;
            text.supportRichText = false;
            
            var it =  new InputText {inputField = inf, autoCompletes= new List<string>() };

            text.RegisterDirtyLayoutCallback(it.ResizeUI);

            return it;
        }

        //public string output = "";


        public void ResizeHeightWithFontSize(int height)
        {
            var sd = (inputField.transform as RectTransform).sizeDelta;
            sd.y = height;
            (inputField.transform as RectTransform).sizeDelta = sd;

            inputField.textComponent.fontSize = UIHelper.GetRecommandFontSizeWithHeight(height);

            //inputField.textComponent.fontSize = height -16;
            //UIHelper.DelayOneFrame(() =>
            //{
            //    var sd = (inputField.transform as RectTransform).sizeDelta;
            //    sd.y = inputField.textComponent.preferredHeight + 4;
            //    (inputField.transform as RectTransform).sizeDelta = sd;
            //    arrayOfFontSize[(int)sd.y] = inputField.textComponent.fontSize;
            //    output+= ("{"+inputField.textComponent.fontSize + ",  " + sd.y+"}, \n");
            //});
        }
    }
}